//笔试强训:买卖股票最好时机

#include <atomic>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

class compare
{
public:
    bool operator()(int* e1, int* e2)
    {
        return *e1 < *e2;
    }
};

int main() {
    int n = 0; cin >> n;
    vector<int> nums(n);
    vector<int*> pnums(n);
    for(int i = 0; i < n; i++)
    {
        cin >> nums[i];
        pnums[i] = &nums[i];
    }
    sort(pnums.begin(), pnums.end(), compare());
    int left = 0, right = n - 1;
    while(left < right)
    {
        if(pnums[left] < pnums[right])
            break;
        
        if(*pnums[left + 1] - *pnums[left] > *pnums[right] - *pnums[right - 1])
            --right;
        else
            ++left;
    }

    cout << *pnums[right] - *pnums[left] << endl;
}
// 64 位输出请用 printf("%lld")